﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy1_10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private bool validateNum(string s)
        {
            Regex reg = new Regex(@"^[0-9]+$");
            if (reg.IsMatch(s)) return true;
            else return false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("操作数不能为空！");
                return;
            }
            if (!validateNum(textBox1.Text) || !validateNum(textBox2.Text))
            {
                MessageBox.Show("操作数必须是无符号整数！");
                return;
            }
            float num1 = float.Parse(textBox1.Text);
            float num2 = float.Parse(textBox2.Text);
            textBox3.Text = (num1 + num2).ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("操作数不能为空！");
                return;
            }
            if (!validateNum(textBox1.Text) || !validateNum(textBox2.Text))
            {
                MessageBox.Show("操作数必须是无符号整数！");
                return;
            }
            float num1 = float.Parse(textBox1.Text);
            float num2 = float.Parse(textBox2.Text);
            textBox3.Text = (num1 - num2).ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("操作数不能为空！");
                return;
            }
            if (!validateNum(textBox1.Text) || !validateNum(textBox2.Text))
            {
                MessageBox.Show("操作数必须是无符号整数！");
                return;
            }
            float num1 = float.Parse(textBox1.Text);
            float num2 = float.Parse(textBox2.Text);
            textBox3.Text = (num1 * num2).ToString();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("操作数不能为空！");
                return;
            }
            if (!validateNum(textBox1.Text) || !validateNum(textBox2.Text))
            {
                MessageBox.Show("操作数必须是无符号整数！");
                return;
            }
            float num2 = float.Parse(textBox2.Text);
            if (num2 == 0)
            {
                MessageBox.Show("除数不得为零！");
                textBox2.Focus();
                return;
            }
            float num1 = float.Parse(textBox1.Text);
            textBox3.Text = (num1 / num2).ToString();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !validateNum(textBox1.Text))
            {
                MessageBox.Show("只能是整数(>=0)，请重设！");
                textBox1.Text = "";
                textBox1.Focus();
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox2.Text) && !validateNum(textBox2.Text))
            {
                MessageBox.Show("只能是整数(>=0)，请重设！");
                textBox2.Text = "";
                textBox2.Focus();
            }
        }
    }
}
